using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

// 定义一个继承自Skill的类Crystal_Skill
public class Crystal_Skill : Skill
{
    [Header("Crtstal simple")]
    [SerializeField] private UI_SkillTreeSlot unlockCrystalButton; // 水晶技能解锁按钮
    public bool crystalUnlocked; // 水晶技能是否解锁

    [Header("Explode crystal")]
    [SerializeField] private UI_SkillTreeSlot unlockExplosiveButton; // 爆炸水晶技能解锁按钮
    [SerializeField] private bool canExplode; // 控制水晶是否可以爆炸

    [Header("Moving crystal")]
    [SerializeField] private UI_SkillTreeSlot unlockMovingCrystalButton; // 移动水晶技能解锁按钮
    [SerializeField] private bool canMoveToenemy; // 控制水晶是否可以移动到敌人
    [SerializeField] private float moveSpeed; // 水晶移动速度

    [Header("Crystal mirage")]
    [SerializeField] private UI_SkillTreeSlot unlockCloneInstaedButton; // 水晶幻影技能解锁按钮
    [SerializeField] private bool cloneInseadOfCrystal; // 能否在瞬移水晶时释放幻影

    [Header("Multi stacking crystal")]
    [SerializeField] private UI_SkillTreeSlot unlockMultiStackButton; // 多重堆叠水晶技能解锁按钮
    [SerializeField] private bool canUseMultiStacks; // 控制是否可以连续释放水晶
    [SerializeField] private int amountOfStacks; // 连续释放水晶的数量
    [SerializeField] private float multiStackCooldown; // 连续释放水晶的总冷却时间
    [SerializeField] private float useTimeWondow; // 未释放完水晶的时间窗口，重装完进入总冷却
    [SerializeField] private List<GameObject> crystalLeft = new List<GameObject>(); // 水晶数量列表

    [Header("Crystal info")]
    [SerializeField] private float crystalDuration = 3f; // 水晶的持续时间
    [SerializeField] private GameObject crystalPrefab; // 水晶的预制体

    private GameObject currentCrystal; // 当前水晶实例

    // 重写Start方法
    protected override void Start()
    {
        base.Start();
        // 为解锁按钮添加点击事件监听器
        unlockCrystalButton.GetComponent<Button>().onClick.AddListener(UnlockCrystal);
        unlockExplosiveButton.GetComponent<Button>().onClick.AddListener(UnlockExplosiveCrystal);
        unlockMovingCrystalButton.GetComponent<Button>().onClick.AddListener(UnlockMovingCrystal);
        unlockCloneInstaedButton.GetComponent<Button>().onClick.AddListener(UnlockCrystalMirage);
        unlockMultiStackButton.GetComponent<Button>().onClick.AddListener(UnlockMultiStack);
    }

    // 重写Update方法
    protected override void Update()
    {
        base.Update();
    }

    #region 使用技能
    // 重写UseSkill方法
    public override void UseSkill()
    {
        base.UseSkill();
        if (canUseMultiCrystal())
            return;
        if (currentCrystal == null) // 如果当前没有水晶
        {
            CreateCrystal(); // 创建水晶
        }
        else // 如果当前有水晶
        {
            if (canMoveToenemy) return; // 如果水晶可以移动，则不进行传送
            Vector2 playerPos = player.transform.position; // 获取玩家当前位置

            player.transform.position = currentCrystal.transform.position; // 玩家与水晶交换位置

            currentCrystal.transform.position = playerPos; // 水晶与玩家交换位置

            if (cloneInseadOfCrystal) // 如果在交换位置时不爆炸而是产生幻影
            {
                SkillManager.instance.clone.CreateClone(currentCrystal.transform, Vector3.zero);
                Destroy(currentCrystal);
            }
            else
            {
                currentCrystal.GetComponent<Crystal_Skill_Controller>()?.FinishCrystal(); // 结束水晶技能
            }
        }
    }

    // 创建水晶
    public void CreateCrystal()
    {
        currentCrystal = Instantiate(crystalPrefab, player.transform.position, Quaternion.identity); // 创建水晶
        Crystal_Skill_Controller currentCrystalScript = currentCrystal.GetComponent<Crystal_Skill_Controller>(); // 获取水晶的脚本
        // 设置水晶的持续时间，是否可以爆炸，是否会移动到敌人身上，移动速度
        currentCrystalScript.SetupCrystal(crystalDuration, canExplode, canMoveToenemy, moveSpeed, FindClosestEnemy(currentCrystal.transform));
    }

    // 随机选择目标
    public void CurrentCrtstalChooseRandomTarget() => currentCrystal.GetComponent<Crystal_Skill_Controller>().ChooseRandomEnemy();

    // 连续使用水晶
    private bool canUseMultiCrystal()
    {
        if (canUseMultiStacks)
        {
            if (crystalLeft.Count > 0)
            {
                // useTimeWondow时间后无论是否用完水晶都要进入冷却
                if (crystalLeft.Count == amountOfStacks)
                {
                    Invoke("ResetAbility", useTimeWondow);
                }
                cooldown = 0; // 还有水晶就不冷却或冷却完毕重置冷却
                GameObject crystalToSpawn = crystalLeft[crystalLeft.Count - 1]; // 选择最后一个水晶Prefab
                GameObject newCrystal = Instantiate(crystalToSpawn, player.transform.position, Quaternion.identity); // 创建水晶
                crystalLeft.Remove(crystalToSpawn); // 用掉一个水晶
                newCrystal.GetComponent<Crystal_Skill_Controller>().
                    SetupCrystal(crystalDuration, canExplode, canMoveToenemy, moveSpeed, FindClosestEnemy(newCrystal.transform));
                if (crystalLeft.Count <= 0) // 没有水晶了
                {
                    cooldown = multiStackCooldown; // 设置冷却等待进入冷却
                    RefilCrystal(); // 重装水晶
                }
                return true;
            }
        }
        return false;
    }

    // 重装水晶
    private void RefilCrystal()
    {
        int amountToAdd = amountOfStacks - crystalLeft.Count;
        for (int i = 0; i < amountToAdd; i++)
        {
            crystalLeft.Add(crystalPrefab);
        }
    }

    private void ResetAbility()
    {
        // 用完水晶进入冷却了就不重新设置冷却
        if (cooldownTimer > 0) return;
        cooldownTimer = multiStackCooldown;
        RefilCrystal();
    }
    #endregion

    #region 解锁技能
    private void UnlockCrystal()
    {
        if (unlockCrystalButton.unlocked)
        {
            crystalUnlocked = true;
        }
    }

    private void UnlockExplosiveCrystal()
    {
        if (unlockExplosiveButton.unlocked)
        {
            canExplode = true;
        }
    }

    private void UnlockMovingCrystal()
    {
        if (unlockMovingCrystalButton.unlocked)
        {
            canMoveToenemy = true;
        }
    }

    private void UnlockCrystalMirage()
    {
        if (unlockCloneInstaedButton.unlocked)
        {
            cloneInseadOfCrystal = true;
        }
    }

    private void UnlockMultiStack()
    {
        if (unlockMultiStackButton.unlocked)
        {
            canUseMultiStacks = true;
        }
    }
    #endregion

    // 重写CheckUnlock方法
    protected override void CheckUnlock()
    {
        UnlockCrystal();
        UnlockExplosiveCrystal();
        UnlockMovingCrystal();
        UnlockCrystalMirage();
        UnlockMultiStack();
    }
}